﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;

namespace AliCMS.CourseModule.OleDb
{
    public class MemberLevel : OleDbBase, Dal.IMemberLevel
    {
        public int GetInnerIDFromGuid(Guid guid)
        {
            Command.Parameters.Clear();
            Command.Parameters.Add("@" + Alias.MemberLevel.ID, MapType(Alias.MemberLevel.ID)).Value = guid;
            Command.CommandText = "SELECT " + MapColumn(Alias.MemberLevel.InnerID) + " FROM " + MapTable(Alias.Objects.MemberLevel) + " WHERE " + MapColumn(Alias.MemberLevel.ID) + "=?";
            object result = Command.ExecuteScalar();
            if (result == null)
                return 0;
            else
                return (int)result;
        }
        public Guid GetGuidFromInnerID(object id)
        {
            Command.Parameters.Clear();
            Command.Parameters.Add("@" + Alias.MemberLevel.InnerID, MapType(Alias.MemberLevel.InnerID)).Value = id;
            Command.CommandText = "SELECT " + MapColumn(Alias.MemberLevel.ID) + " FROM " + MapTable(Alias.Objects.MemberLevel) + " WHERE " + MapColumn(Alias.MemberLevel.InnerID) + "=?";
            object result = Command.ExecuteScalar();
            if (result == null)
                return Guid.Empty;
            else
                return (Guid)result;
        }
        public Dictionary<string, object> Get(object id)
        {
            Dictionary<string, object> result = new Dictionary<string, object>();

            StringBuilder sql = new StringBuilder("SELECT TOP 1");
            sql.Append(" " + MapColumn(Alias.MemberLevel.ID));
            sql.Append("," + MapColumn(Alias.MemberLevel.InnerID));
            sql.Append("," + MapColumn(Alias.MemberLevel.Name, Lang));
			sql.Append("," + MapColumn(Alias.MemberLevel.Fee));
			sql.Append("," + MapColumn(Alias.MemberLevel.TimeUnit));
			sql.Append("," + MapColumn(Alias.MemberLevel.TimeAmount));
			sql.Append("," + MapColumn(Alias.MemberLevel.Commision));
			sql.Append("," + MapColumn(Alias.MemberLevel.InitialNo));
			sql.Append("," + MapColumn(Alias.MemberLevel.CanApply));
			sql.Append("," + MapColumn(Alias.MemberLevel.Agreement, Lang));
			sql.Append("," + MapColumn(Alias.MemberLevel.Sort));
            sql.Append(" FROM " + MapTable(Alias.Objects.MemberLevel));

            if (id is Guid)
            {
                sql.Append(" WHERE " + MapColumn(Alias.MemberLevel.ID) + "=?");

                Command.Parameters.Clear();
                Command.Parameters.Add("@" + Alias.MemberLevel.ID, MapType(Alias.MemberLevel.ID)).Value = id;
            }
            else
            {
                sql.Append(" WHERE " + MapColumn(Alias.MemberLevel.InnerID) + "=?");

                Command.Parameters.Clear();
                Command.Parameters.Add("@" + Alias.MemberLevel.InnerID, MapType(Alias.MemberLevel.InnerID)).Value = id;
            }

            Command.CommandText = sql.ToString();
            DataReader = Command.ExecuteReader();
            if (!DataReader.HasRows)
            {
                DataReader.Close();
                return null;
            }

            DataReader.Read();
            int i = 0;
            result[Alias.MemberLevel.ID] = DataReader[i++];
            result[Alias.MemberLevel.InnerID] = DataReader[i++];
            result[Alias.MemberLevel.Name] = DataReader[i++];
			result[Alias.MemberLevel.Fee] = DataReader[i++];
			result[Alias.MemberLevel.TimeUnit] = DataReader[i++];
			result[Alias.MemberLevel.TimeAmount] = DataReader[i++];
			result[Alias.MemberLevel.Commision] = DataReader[i++];
			result[Alias.MemberLevel.InitialNo] = DataReader[i++];
			result[Alias.MemberLevel.CanApply] = DataReader[i++];
			result[Alias.MemberLevel.Agreement] = DataReader[i++];
			result[Alias.MemberLevel.Sort] = DataReader[i++];
            DataReader.Close();

            return result;
        }
        public void Add(Dictionary<string, object> item)
        {
            Command.Parameters.Clear();

            #region 排序
			if (!item.ContainsKey(Alias.MemberLevel.Sort))
			{
				Command.CommandText = "SELECT MAX(" + MapColumn(Alias.MemberLevel.Sort) + ") + 1 FROM " + MapTable(Alias.Objects.MemberLevel);
				object sort = Command.ExecuteScalar();
				if (sort == DBNull.Value)
					item.Add(Alias.MemberLevel.Sort, 1);
				else
					item.Add(Alias.MemberLevel.Sort, sort);
			}
            #endregion

            #region 设置RowLang
            item.Add(Alias.MemberLevel.RowLang, "|" + Lang + "|");
            #endregion

            StringBuilder names = new StringBuilder();
            StringBuilder values = new StringBuilder();

            Command.Parameters.Clear();
            PrepareParameters(names, values, item);

            Command.CommandText = "INSERT INTO " + MapTable(Alias.Objects.MemberLevel) + "(" + names + ") VALUES(" + values + ")";
            Command.ExecuteNonQuery();
        }
        public void Modify(int id, Dictionary<string, object> changes)
        {
            Command.Parameters.Clear();
            Command.Parameters.Add("@" + Alias.MemberLevel.InnerID, MapType(Alias.MemberLevel.InnerID)).Value = id;

            #region 检查并设置RowLang
            Command.CommandText = "SELECT " + MapColumn(Alias.MemberLevel.RowLang) + " FROM " + MapTable(Alias.Objects.MemberLevel) + " WHERE " + MapColumn(Alias.MemberLevel.InnerID) + "=?";
            string rowLang = Command.ExecuteScalar().ToString();
            if (!rowLang.Contains("|" + Lang + "|"))
                changes.Add(Alias.MemberLevel.RowLang, rowLang + Lang + "|");
            #endregion

            StringBuilder sql = new StringBuilder("UPDATE " + MapTable(Alias.Objects.MemberLevel) + " SET ");
            Command.Parameters.Clear();
            PrepareParameters(sql, changes);
            Command.Parameters.Add("@" + Alias.MemberLevel.InnerID, MapType(Alias.MemberLevel.InnerID)).Value = id;

            Command.CommandText = sql + " WHERE " + MapColumn(Alias.MemberLevel.InnerID) + "=?";
            Command.ExecuteNonQuery();
        }
        public void Delete(int id)
        {
            Command.Parameters.Clear();
            Command.Parameters.Add("@" + Alias.MemberLevel.InnerID, MapType(Alias.MemberLevel.InnerID)).Value = id;

            StringBuilder sql = new StringBuilder("DELETE FROM " + MapTable(Alias.Objects.MemberLevel) + " WHERE " + MapColumn(Alias.MemberLevel.InnerID) + "=?");
            Command.CommandText = sql.ToString();
            Command.ExecuteNonQuery();
        }
        public DataTable List(int page, int pageSize, string condition, string sort)
        {
            DataTable result = new DataTable();
            result.Columns.Add(Alias.MemberLevel.ID, MapCSType(Alias.MemberLevel.ID));
            result.Columns.Add(Alias.MemberLevel.InnerID, MapCSType(Alias.MemberLevel.InnerID));
            result.Columns.Add(Alias.MemberLevel.Name, MapCSType(Alias.MemberLevel.Name));
			result.Columns.Add(Alias.MemberLevel.Fee, MapCSType(Alias.MemberLevel.Fee));
			result.Columns.Add(Alias.MemberLevel.TimeUnit, MapCSType(Alias.MemberLevel.TimeUnit));
			result.Columns.Add(Alias.MemberLevel.TimeAmount, MapCSType(Alias.MemberLevel.TimeAmount));
			result.Columns.Add(Alias.MemberLevel.Commision, MapCSType(Alias.MemberLevel.Commision));
			result.Columns.Add(Alias.MemberLevel.InitialNo, MapCSType(Alias.MemberLevel.InitialNo));
			result.Columns.Add(Alias.MemberLevel.CanApply, MapCSType(Alias.MemberLevel.CanApply));
			result.Columns.Add(Alias.MemberLevel.Agreement, MapCSType(Alias.MemberLevel.Agreement));
			result.Columns.Add(Alias.MemberLevel.Sort, MapCSType(Alias.MemberLevel.Sort));

            #region 条件
            Command.Parameters.Clear();
            StringBuilder where = new StringBuilder();
            if (!String.IsNullOrEmpty(condition))
            {
                where.Append(" WHERE " + Alias.MemberLevel.RowLang + " LIKE '%|" + Lang + "|%' AND ");
                where.Append("(" + condition + ")");
                ParseCondition(where, "");
            }
            else
            {
                where.Append(" WHERE " + MapColumn(Alias.MemberLevel.RowLang) + " LIKE '%|" + Lang + "|%'");
            }
            #endregion

            #region 排序
            if (!String.IsNullOrEmpty(sort))
                sort = " ORDER BY " + ParseSort(sort, "");
            else
                sort = " ORDER BY " + MapColumn(Alias.MemberLevel.Sort) + " ASC";
            #endregion

            #region 构造SQL语句
            StringBuilder sql = new StringBuilder("SELECT");
            if (page > 0 && pageSize > 0)
                sql.Append(" TOP " + (page * pageSize));
            sql.Append(" " + MapColumn(Alias.MemberLevel.ID));
            sql.Append("," + MapColumn(Alias.MemberLevel.InnerID));
            sql.Append("," + MapColumn(Alias.MemberLevel.Name, Lang));
			sql.Append("," + MapColumn(Alias.MemberLevel.Fee));
			sql.Append("," + MapColumn(Alias.MemberLevel.TimeUnit));
			sql.Append("," + MapColumn(Alias.MemberLevel.TimeAmount));
			sql.Append("," + MapColumn(Alias.MemberLevel.Commision));
			sql.Append("," + MapColumn(Alias.MemberLevel.InitialNo));
			sql.Append("," + MapColumn(Alias.MemberLevel.CanApply));
			sql.Append("," + MapColumn(Alias.MemberLevel.Agreement, Lang));
			sql.Append("," + MapColumn(Alias.MemberLevel.Sort));
            sql.Append(" FROM " + MapTable(Alias.Objects.MemberLevel));
            sql.Append(where + sort);
            Command.CommandText = sql.ToString();
            #endregion

            #region 执行SQL语句并获取记录
            int curIndex = 0, rowIndex = 0;
            int startIndex = (page - 1) * pageSize;
            DataReader = Command.ExecuteReader();
            while (DataReader.Read())
            {
                if (pageSize != -1)
                {
                    if (curIndex < startIndex)
                    {
                        curIndex++;
                        continue;
                    }

                    if (rowIndex == pageSize)
                        break;
                }

                DataRow row = result.NewRow();
                int i = 0;
                row[Alias.MemberLevel.ID] = DataReader[i++];
                row[Alias.MemberLevel.InnerID] = DataReader[i++];
				row[Alias.MemberLevel.Name] = DataReader[i++];
				row[Alias.MemberLevel.Fee] = DataReader[i++];
				row[Alias.MemberLevel.TimeUnit] = DataReader[i++];
				row[Alias.MemberLevel.TimeAmount] = DataReader[i++];
				row[Alias.MemberLevel.Commision] = DataReader[i++];
				row[Alias.MemberLevel.InitialNo] = DataReader[i++];
				row[Alias.MemberLevel.CanApply] = DataReader[i++];
				row[Alias.MemberLevel.Agreement] = DataReader[i++];
				row[Alias.MemberLevel.Sort] = DataReader[i++];
                result.Rows.Add(row);

                rowIndex++;
            }
            DataReader.Close();
            #endregion

            return result;
        }
        public int GetRecordCount(string condition)
        {
            #region 条件
            Command.Parameters.Clear();
            StringBuilder where = new StringBuilder();
            if (!String.IsNullOrEmpty(condition))
            {
                where.Append(" WHERE " + Alias.MemberLevel.RowLang + " LIKE '%|" + Lang + "|%' AND ");
                where.Append("(" + condition + ")");
                ParseCondition(where, "");
            }
            else
            {
                where.Append(" WHERE " + MapColumn(Alias.MemberLevel.RowLang) + " LIKE '%|" + Lang + "|%'");
            }
            #endregion

            Command.CommandText = "SELECT COUNT(*) FROM " + MapTable(Alias.Objects.MemberLevel) + where;
            return Convert.ToInt32(Command.ExecuteScalar());
        }
    }
}